package com.imc.platform.base.wxOauth2Token.service.impl;

import com.imc.platform.base.user.dao.repository.UserRepository;
import com.imc.platform.base.user.pojo.User;
import com.imc.platform.base.user.service.ISSOService;
import com.imc.platform.base.wxOauth2Token.dao.IOauthMapper;
import com.imc.platform.base.wxOauth2Token.service.IOauthService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Map;

@Service
public class OauthServiceImpl implements IOauthService {

    @Autowired
    IOauthMapper oauthMapper;
    @Autowired
    PasswordEncoder passwordEncoder;
    @Autowired
    ISSOService ssoservice;

    @Autowired
    UserRepository userRepository;

    @Override
    public String addWXLogin(User user) {
        //根据手机号码查询微信登陆表获取角色ID
        Map<String,Object>retMap=oauthMapper.queryRoleIdFromWXLoginByTel(user.getLoginName());
        if(retMap!=null&&!StringUtils.isEmpty(retMap.get("duty").toString())){
            ssoservice.SSOLogin(user.getLoginName());
            return retMap.get("duty").toString();
        }
        user.setDuty("student");
        user.setSex("1");
        user.setLoginPwd(passwordEncoder.encode("88888888"));
//        userRepository.save(user);
        if(ssoservice.addWXLogin(user)){
            ssoservice.SSOLogin(user.getLoginName());
        }
        return user.getDuty();
    }

}
